package com.wanbatv.wanba.jianshencao.util;

import android.media.MediaPlayer;
import android.util.Log;
import android.util.StringBuilderPrinter;


import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by Forcs on 15/6/10.
 */
public class WanbaMediaPlayer extends MediaPlayer {

    private OkHttpClient mHttpClient = null;

    public interface OnCheckFinishCallback {

        public void onCheckFinish(boolean success);
    }

    public WanbaMediaPlayer() {
        super();
    }

    /**
     * 设置远程播放地址
     * @param url 播放地址
     * @throws IOException
     */
    public void setUrl(String url, OnCheckFinishCallback callback) throws IOException {
        setUrl(url, true, callback);
    }

    /**
     * 设置远程播放地址，并配置是否需要检查重定向地址
     * @param url 播放地址
     * @param check true 通过内部的HttpClient检查重定向地址，否则，不检查
     * @throws IOException
     */
    public void setUrl(final String url, boolean check, final OnCheckFinishCallback callback) throws IOException {
        if (check) {    //检查地址是否重定向
            if (mHttpClient == null) {
                mHttpClient = new OkHttpClient();
                mHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);
                mHttpClient.setReadTimeout(30, TimeUnit.SECONDS);
                mHttpClient.setWriteTimeout(30, TimeUnit.SECONDS);
            }
            //请求
            final Request request = new Request.Builder()
                    .url(url)
                    .build();
            mHttpClient.newCall(request)
                    .enqueue(new Callback() {
                        @Override
                        public void onFailure(Request request, IOException e) {
                            Log.d("WanbaMediaPlayer","@@@ onFailure");
                            try {
                                setDataSource(url);
                            }catch (Exception e1){
                                e1.printStackTrace();
                            }
                            e.printStackTrace();
                            if (callback != null) {
                                callback.onCheckFinish(false);
                            }
                        }

                        @Override
                        public void onResponse(Response response) throws IOException {
                            Set<String> hs = response.headers().names();
                            Iterator<String> it = hs.iterator();
                            while (it.hasNext()) {
                                Log.d("WanbaMediaPlayer", "@@@ onResonse header " + response.header(it.next()));
                            }
                            Log.d("WanbaMediaPlayer", "@@@ onResponse " + response.header("Location", url) + ", sc " + response.code());
                            if (response.isRedirect()) {    //如果重定向，就用重定向地址进行播放
                                Log.d("WanbaMediaPlayer", "@@@ redirect " + response.header("Location", url));
                                setDataSource(response.header("Location", url));
                            } else {    //用传递过来的地址进行播放
                                Log.d("WanbaMediaPlayer", "@@@ direct " + url);
                                try {
                                    setDataSource(url);
                                }catch (IllegalStateException e){
                                    e.printStackTrace();
                                }
                            }
                            if (callback != null) {
                                callback.onCheckFinish(true);
                            }
                        }
            });


        } else {    //不检查就直接播放
            setDataSource(url);
            if (callback != null) {
                callback.onCheckFinish(true);
            }
        }
    }

}
